package dao;

import vo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Userdao {
    public User getUserByUserName(String username) throws SQLException {
        User user=null;
        Connection conn= Base.getConnection();
        String sql="select * from user where username = ?";
        PreparedStatement pst= conn.prepareStatement(sql);
        pst.setString(1,username);
        ResultSet set= pst.executeQuery();
        if(set.next()){
            user=new User();
            user.setUsername(set.getString(1));
            user.setPassword(set.getString(2));
        }
        Base.close(conn,pst,set);
        return user;
    }
    public void setUser(User user) throws SQLException {
        Connection conn = Base.getConnection();
        String sql = "INSERT INTO user(username,password)VALUES (?,?)";
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setString(1, user.getUsername());
        pst.setString(2, user.getPassword());
        pst.executeUpdate();
    }
}
